using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ItemSpace;
using UnityEngine.Rendering.Universal;
public class item : MonoBehaviour
{
    Item myInfo;
    public LayerMask checkLayer;
    public ItemsBox box;
    public float waitTime;
    public Rigidbody2D MyRigidBody;
    private bool getFlag = true;
    public Light2D lightBody;
    private Color color;
    public void Awake()
    {
        box=GameObject.Find("ItemsBox").GetComponent<ItemsBox>();
        StartCoroutine(ResetLayerMask());
        
    }
    public Color GetMyColor()
    {
        Color color = Color.black;
        for (int i = 0; i < QualityClass.itemsInfo.Length; i++)
        {
            if (myInfo.Quality == QualityClass.itemsInfo[i].Quality)
            {
                color = QualityClass.itemsInfo[i].color;
                break;
            }
        }
        return color;
    }
    IEnumerator ResetLayerMask()
    {
        yield return new WaitForSeconds(waitTime);
        gameObject.layer = LayerMask.NameToLayer("Object");
    }
    public void SetMyInfo(Item item)
    {
        myInfo = new Item(item);
        Sprite sprite = null;
        box.GetItemSprite(myInfo.Id, out sprite);
        GetComponent<SpriteRenderer>().sprite = sprite;
    }
    
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player"&&getFlag )
        {
            getFlag = false;
            Player player = collision.gameObject.GetComponent<Player>();
            player.GetItem(myInfo);
            Destroy(gameObject);
        }
        if(collision.gameObject.tag == "Ground")
        {
            MyRigidBody.velocity=new Vector2(0,MyRigidBody.velocity.y);
        }
    }
    private void Start()
    {
        
        lightBody.color= GetMyColor();

    }
}
